package com.qiangesoft.datasourcepro.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.datasourcepro.core.DataSource;
import com.qiangesoft.datasourcepro.core.DataSourceContext;
import com.qiangesoft.datasourcepro.entity.BcgDataSource;
import com.qiangesoft.datasourcepro.mapper.BcgDataSourceMapper;
import com.qiangesoft.datasourcepro.service.IBcgDataSourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据源 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @since 2024-03-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BcgBcgDataSourceServiceImpl extends ServiceImpl<BcgDataSourceMapper, BcgDataSource> implements IBcgDataSourceService {

    @Override
    public List<BcgDataSource> changeDataSource(String datasourceId) {
        try {
            DataSourceContext.setDataSource(datasourceId);
            return this.list();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("数据源切换失败");
        } finally {
            DataSourceContext.removeDataSource();
        }
    }

    @DataSource(value = "1")
    @Override
    public List<BcgDataSource> changeDataSourceByAnnotation() {
        return this.list();
    }
}
